<template>
  <div>
    <el-container>
      <el-header>
        <!-- 图标 -->
        <div class="loginBox">
          <img src="../assets/heima.png" alt="" />
          <span>电商后台管理平台</span>
        </div>
        <!-- 退出 -->
        <el-button type="info" @click="quit">退出</el-button>
      </el-header>
      <el-container>
        <el-aside :width="isCollapse == true ? '64px' : '200px'">
          <!-- 侧边栏导航 -->
          <el-menu
            background-color="#333744"
            text-color="#fff"
            active-text-color="#3C89DA"
            unique-opened
            :collapse="isCollapse"
            :collapse-transition="false"
            :default-active="activePath"
            router
          >
            <!-- 折叠效果 -->
            <div class="toggle" @click="toggleColl">|||</div>
            <!-- 一级导航 -->
            <el-submenu
              :index="item.id + ''"
              v-for="item in navList"
              :key="item.id"
            >
              <template slot="title">
                <i :class="iconObj[item.id]"></i>
                <span>{{ item.authName }}</span>
              </template>
              <!-- 二级导航 -->
              <el-menu-item
                :index="'/' + subItem.path"
                v-for="subItem in item.children"
                :key="subItem.id"
                @click="navState('/' + subItem.path)"
              >
                <i class="el-icon-menu"></i>
                <span>{{ subItem.authName }}</span>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <!-- 路由占位符 -->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data() {
    return {
      navList: [],
      iconObj: {
        '125': 'iconfont icon-users',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      isCollapse: false,
      activePath: ''
    }
  },
  created() {
    // 获取侧边栏数据
    this.getNav()
    // 获取高亮的activePath
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    // 每一个请求都要token令牌才能访问后台数据
    async getNav() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      console.log(res.data)
      this.navList = res.data
    },
    // 退出系统
    quit() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 侧边栏切换效果
    toggleColl() {
      this.isCollapse = !this.isCollapse
    },
    // 路由模式下导航栏高亮
    navState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.el-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
  height: 60px;
  background-color: #373d41;
  color: #fff;
  font-size: 20px;
}

.loginBox {
  display: flex;
  align-items: center;
  span {
    margin-left: 10px;
  }
}

.el-aside {
  height: 600px;
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}

.toggle {
  background-color: #4a5064;
  font-size: 12px;
  text-align: center;
  color: #fff;
  letter-spacing: 0.2em;
  line-height: 24px;
  cursor: pointer;
}

.iconfont {
  margin-right: 10px;
}

.el-main {
  background-color: #eaedf1;
}
</style>
